$(function ()
{
    //根据pageurl从authority获取相应的权限---custom_loadCookie.js
    var authorityString=getAuthorityByMenuName("系统权限");
    //根据权限设置按钮的显示和隐藏---custom_Common.js
    IsNoneOrBlock(authorityString,"Auth");
    var dg;//系统权限的datagird
    var editRow = undefined;
    var rowEditor = undefined;
    dg = $("#tb").treegrid(
        {
            url : "../../api/nj_getAllAuthorityInfo_Treegrid", //加载的URL
            method : "get",
            idField : 'id',
            treeField:'authorityname',
            parentField : 'parentid',
            iconCls: 'icon-ok',
            animate:true, 
            rownumbers:true,
            singleSelect:true,
            striped:true,
            enableHeaderClickMenu: false,
            enableHeaderContextMenu: false,
            enableRowContextMenu: false,
            toolbar:'#toolBar',
            dataPlain: true,
            columns : [[//每个列具体内容

                    {
                        field : 'id',
                        title : 'id',
                        hidden:true
                    },
                    {
                        field : 'authorityname',
                        title : '权限名称',
                        width : 130,
                        align : 'center'
                    },
                    {
                        field : 'flag',
                        title : '是否可用',
                        width : 100,
                        align : 'center',
                        formatter:function(value , record , index){
                            if(value == 1){
                                return '<span style=color:red; >可用</span>' ;
                            }else{
                                return '<span style=color:blue; >不可用</span>' ; 
                            }
                        }
                    },
                    {
                        field : 'pageurl',
                        title : '页面URL',
                        width : 100,
                        align : 'center'
                    },
                    {
                        field : 'parentid',
                        title : '父节点',
                        width : 100,
                        align : 'center'
                    },

                    {
                        field : 'authority',
                        title : '权限列表',
                        width : 300,
                        align : 'center'
                    },
                     {
                        field : 'adduser',
                        title : '添加人',
                        width : 100,
                        align : 'center'
                    },
                    {
                        field : 'addtime',
                        title : '添加时间',
                        width : 180,
                        align : 'center',
                        formatter:function(val,row){
                          var now = new Date(val);
                          return now.format("yyyy-MM-dd hh:mm:ss");
                        }
                    },
                    {
                        field : 'operate',
                        title : '操作',
                        width : 100,
                        align : 'center',

                        formatter : function (value, row, index)
                        {
                            var row1 = dg.datagrid('getData').rows[index];
                            var url = "nothing";
                            var str = '<a href="#" name="opera2" class="easyui-linkbutton" ></a>';
                            return str;
                        }
                    },
                ]],
            onLoadSuccess : function (data)
            {
                $("a[name='opera2']").linkbutton(
                {
                    text : '>>>',
                    plain : true,
                    iconCls : 'icon-search'
                }
                );
            }
         }
        );

    

  
    //保存按钮的点击事件
    $("#save").click(function ()
    {
        
    }
    );

    //取消按钮的点击事件
    $("#cancel").click(function ()
    {
        $('#editinfo').dialog('close');
    }
    );


    //1.增加系统权限
    $("#addAuth").click(function ()
    {
        var xposition = screen.width / 2 - 450;

        $('#manage2').hide();
        $('#manage1').show();
        $('#editinfo').form('clear');
        $('#editinfo').dialog('setTitle', '增加');
        $('#editinfo').window('open').window('move',
        {
            left : xposition,
            top : 0
        }
        );

        $("#editinfo").form("load",
        {
            adduser : realname,
        }
        );
    }
    );

    //2.删除系统权限
    $("#deleteAuth").click(function ()
    {
        var rows = dg.datagrid('getSelections');
        if (rows.length <= 0)
        {
            $.messager.alert('警告', '您没有选择', 'error');
        }
        else if (rows.length > 1)
        {
            $.messager.alert('警告', '不支持批量删除', 'error');
        }
        else
        {
            var index = dg.datagrid('getRowIndex', dg.datagrid('getSelected'))
                var row1 = dg.datagrid('getData').rows[index];
            $.messager.confirm('确定', '您确定要删除吗', function (t)
            {
                if (t)
                {

                    $.ajax(
                    {

                        url : '../../api/deleteAuthorityInfo?id=' + row1.authorityid,
                        // data : {departmentid:row1.departmentid},
                        // dataType : 'json',
                        success : function (r)
                        {
                            if (r == 1)
                            {
                                dg.datagrid('acceptChanges');
                                editRow = undefined;
                                dg.datagrid('reload');
                            }
                            else if (r == 2)
                            {

                                $.messager.alert('警告', '该权限下有子权限，不能删除！', '该权限下有子权限，不能删除！');
                            }
                            else
                            {

                                dg.datagrid('beginEdit', editRow);
                                $.messager.alert('错误', r.msg, 'error');
                            }
                            dg.datagrid('unselectAll');
                        }
                    }
                    );

                }
            }
            )
        }
    }
    );

    //3.修改系统权限
    $("#edit").click(function ()
    {
        var rows = dg.datagrid('getSelections');
        if (rows.length <= 0)
        {
            $.messager.alert('警告', '您没有选择', 'error');
        }
        else if (rows.length == 1)
        {
            var index = dg.datagrid('getRowIndex', dg.datagrid('getSelected'));
            var row1 = dg.datagrid('getData').rows[index];
            window.open("../../menu/authManage/updateauthority?authorityid=" + row1.authorityid + "&authorityname=" + row1.authorityname + "&flag=" + row1.flag + "&authority=" + row1.authority + "&pageurl=" + row1.pageurl + "&parentid=" + row1.parentid + "&addtime=" + row1.addtime + "&adduser=" + row1.adduser, '修改权限', 'height=1200, width=1000, top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=n o, status=no');
        }
        else
        {
            $.messager.alert('警告', '不支持批量修改', 'error');
        }
    }
    );

}
);
